package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwebT;
import cn.e4j.modular.crm.mapper.GwebTMapper;
import cn.e4j.modular.crm.service.GwebTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-16
 */
@Service
public class GwebTServiceImpl extends ServiceImpl<GwebTMapper, GwebT> implements GwebTService {
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwebDtos = this.baseMapper.listPage(page, dto);

        return gwebDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwakList(Dto dto) {

        List<Map<String, Object>> gwakDtos = this.baseMapper.getGwakList(dto);

        return gwakDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getOrgList(Dto dto) {

        List<Map<String, Object>> orgDtos = this.baseMapper.getOrgList(dto);

        return orgDtos;
    }

    @Transactional(readOnly = true)
    public List<Dto> getOrg(Dto dto) {

        List<Dto> orgDtos = this.baseMapper.getOrg(dto);

        return orgDtos;
    }

    @Transactional(readOnly = true)
    public GwebT getByKey(String GWEB001) {
        GwebT gweb = this.baseMapper.getByKey(GWEB001);
        return gweb;
    }

    @Transactional
    @Override
    public void add(GwebT gwebT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto dto= InvokeUtil.invokeBeanMethod("gwebTMapper,getMaxGweb001");
        gwebT.setGWEB001(dto.getString("GWEB001"));
        gwebT.setGWEBLEGAL(shiroUser.getDBC());
        gwebT.setGWEBACTI(gwebT.getGWEB011());
        gwebT.insert();
    }

    @Transactional
    @Override
    public void edit(GwebT gweb) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gweb.setGWEBMODU(shiroUser.getACCOUNT());
        gweb.setGWEBDATE(LocalDate.now());
        gweb.setGWEBTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        UpdateWrapper<GwebT> gwebTUpdateWrapper = new UpdateWrapper<>();
        gwebTUpdateWrapper.eq("GWEB001", gweb.getGWEB001());
        gweb.update(gwebTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delete(GwebT gwebT) {
        UpdateWrapper<GwebT> gwebTUpdateWrapper = new UpdateWrapper<>();
        gwebTUpdateWrapper.eq("GWEB001", gwebT.getGWEB001());
        gwebT.delete(gwebTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwebT> gwebTList) {
        for (GwebT gwebT : gwebTList) {
            UpdateWrapper<GwebT> gwebTUpdateWrapper = new UpdateWrapper<>();
            gwebTUpdateWrapper.eq("GWEB001", gwebT.getGWEB001());
            gwebT.delete(gwebTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public Dto getGwak(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = this.baseMapper.getGwak(dto);
        return pDto;
    }

    @Transactional(readOnly = true)
    public Dto getGwam(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = this.baseMapper.getGwam(dto);
        return pDto;
    }
}
